home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / vol15n10.zip / MULTIL.ZIP / MLSRC.ZIP / MSDEV / PROJECTS / MLAUNCH / REGISTRY.CPP < prev    next >
C/C++ Source or Header  |  1996-03-11  |  3KB  |  144 lines

  1. // Registry helper functions
  2.  
  3. #include "StdAfx.h"
  4. #include "Registry.h"
  5.  
  6. // Implementation of CRegistryKey
  7.  
  8. // Default constructor
  9. CRegistryKey::CRegistryKey()
  10. {
  11. }
  12.  
  13. // Go through the linked list and destroy all CRegistryValue objects found
  14. CRegistryKey::~CRegistryKey()
  15. {
  16.     POSITION pos;
  17.  
  18.     for( pos = m_RegistryValues.GetHeadPosition(); pos != NULL; )
  19.         delete (CRegistryValue*) m_RegistryValues.GetNext(pos);
  20. }
  21.  
  22. // Initialize object with a new key
  23. CRegistryKey::CRegistryKey(CString strRegistryKey)
  24. {
  25.     ASSERT(strRegistryKey != "");
  26.  
  27.     int i = 0;
  28.     HKEY hKey;
  29.     long lResult;
  30.     CString strName, strValue;
  31.     CRegistryValue* RegistryValue;
  32.     DWORD dwlenName, dwlenValue, dwType;
  33.     
  34.     m_strRegistryKey = strRegistryKey;
  35.  
  36.     // Open the desired key
  37.     if (RegOpenKeyEx(HKEY_CLASSES_ROOT, strRegistryKey, 0, KEY_ALL_ACCESS, &hKey)
  38.         == ERROR_SUCCESS) {
  39.  
  40.         // Enumerate the values under MultiLaunch key
  41.         dwlenName = 100;
  42.         dwlenValue = 100;
  43.  
  44.         lResult = RegEnumValue(hKey, i++, strName.GetBuffer(dwlenName), &dwlenName,
  45.             0, &dwType,    (BYTE*) strValue.GetBuffer(dwlenValue), &dwlenValue);
  46.  
  47.         while (lResult != ERROR_NO_MORE_ITEMS) {
  48.  
  49.             strName.ReleaseBuffer();
  50.             strValue.ReleaseBuffer();
  51.  
  52.             // Add to my linked list
  53.             RegistryValue = new CRegistryValue(strName, strValue);
  54.             m_RegistryValues.AddTail((CObject*) RegistryValue);
  55.  
  56.             dwlenName = 100;
  57.             dwlenValue = 100;
  58.             
  59.             lResult = RegEnumValue(hKey, i++, strName.GetBuffer(dwlenName), &dwlenName,
  60.                 0, &dwType,    (BYTE*) strValue.GetBuffer(dwlenValue), &dwlenValue);
  61.         }
  62.         strName.ReleaseBuffer();
  63.         strValue.ReleaseBuffer();
  64.  
  65.         RegistryValue = new CRegistryValue(strName, strValue);
  66.         m_RegistryValues.AddTail((CObject*) RegistryValue);
  67.  
  68.         // Initialize the list
  69.         GotoHead();
  70.     }
  71. }
  72.  
  73. // Goto head of the list
  74. void CRegistryKey::GotoHead()
  75. {
  76.     m_pos = m_RegistryValues.GetHeadPosition();
  77. }
  78.  
  79. // Get next element in the list
  80. BOOL CRegistryKey::GetNext(CRegistryValue*& pRegistryValue)
  81. {
  82.     BOOL bResult = TRUE;
  83.  
  84.     if (m_RegistryValues.GetCount() > 0)
  85.         pRegistryValue = (CRegistryValue*) m_RegistryValues.GetNext(m_pos);
  86.     else
  87.         return FALSE;
  88.  
  89.     if (m_pos == NULL)
  90.         bResult = FALSE;
  91.  
  92.     return bResult;
  93. }
  94.  
  95. // Get element at a certain offset into the list (0-based)
  96.  
  97. CRegistryValue* CRegistryKey::GetAt(int iIndex)
  98. {
  99.     CRegistryValue* pRegistryValue = NULL;
  100.  
  101.     POSITION pos = m_RegistryValues.FindIndex(iIndex);
  102.  
  103.     if (pos != NULL)
  104.         pRegistryValue = (CRegistryValue*) m_RegistryValues.GetAt(pos);
  105.     
  106.     return pRegistryValue;
  107. }
  108.  
  109. // Remove element at a certain offset into the list (0-based)
  110.  
  111. void CRegistryKey::RemoveAt(int iIndex)
  112. {
  113.     POSITION pos = m_RegistryValues.FindIndex(iIndex);
  114.  
  115.     if (pos != NULL)
  116.         m_RegistryValues.RemoveAt(pos);
  117. }
  118.  
  119. // Get count of the number of items in the list
  120.  
  121. int CRegistryKey::GetCount()
  122. {
  123.     return m_RegistryValues.GetCount();
  124. }
  125.  
  126. // Implementation of CRegistryValue
  127.  
  128. // Default constructor
  129. CRegistryValue::CRegistryValue()
  130. {
  131. }
  132.  
  133. // Default destructor
  134. CRegistryValue::~CRegistryValue()
  135. {
  136. }
  137.  
  138. // Initialize object with a new registry value
  139. CRegistryValue::CRegistryValue(CString strName, CString strValue)
  140. {
  141.     m_strName = strName;
  142.     m_strValue = strValue;
  143. }
  144.